package com.pitaya.avatar.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.pitaya.avatar.dao.ISchoolDAO;
import com.pitaya.avatar.domain.Area;
import com.pitaya.avatar.domain.House;
import com.pitaya.avatar.domain.School;
import com.pitaya.avatar.service.IClusterNode;
import com.pitaya.avatar.service.IClusterService;
import com.pitaya.avatar.service.ISchoolService;
import com.pitaya.avatar.util.StringMatcher;

public class SchoolService implements ISchoolService {

	private ISchoolDAO schoolDAO;
	private IClusterService clusterService;
	private StringMatcher stringMatcher;

	public StringMatcher getStringMatcher(){
		return this.stringMatcher;
	}
	
	public void setStringMatcher(StringMatcher matcher){
		this.stringMatcher = matcher;
	}
	
	public IClusterService getClusterService() {
		return clusterService;
	}

	public void setClusterService(IClusterService clusterService) {
		this.clusterService = clusterService;
	}

	public void setSchoolDAO(ISchoolDAO schoolDAO) {
		this.schoolDAO = schoolDAO;
	}
	

	public boolean add(School school) {
		return schoolDAO.insert(school);
	}

	public List<School> find(School school) {
		return schoolDAO.query(school);
	}

	public boolean remove(School school) {
		
		return schoolDAO.delete(school);
	}

	public boolean renew(School school) {
		
		return schoolDAO.update(school);
	}
	
	public List<School> getAllSchoolsByCity(String city){
		School sch = new School();
		sch.setName("南京市琅琊路小学");
		sch.setLetters("nan!jing!shi!lang!ya!lu!xiao!xue");
		sch.setLongitude(118.773404);
		sch.setLatitude(32.067283);
		sch.setPid(139426L);
		List<School> schools = new ArrayList<School>();
		schools.add(sch);
		
		sch = new School();
		sch.setName("南京市北京东路小学");
		sch.setLetters("nan!jing!shi!bei!jing!dong!lu!xiao!xue");
		sch.setLongitude(118.805888);
		sch.setLatitude(32.063994);
		sch.setPid(137926L);
		schools.add(sch);
		return schools;
	}
	
	public List<House> getHouseBySchool(School school){
		ArrayList<House> houses = new ArrayList<House>();
		for(int i=0; i<5; i++){
			House house = new House();
			houses.add(house);
		}
		houses.get(0).setPid(1l);
		houses.get(0).setName("a");
		houses.get(0).setLongitude(118.773504);
		houses.get(0).setLatitude(32.067383);
		
		houses.get(1).setPid(2l);
		houses.get(1).setName("b");
		houses.get(1).setLongitude(118.773604);
		houses.get(1).setLatitude(32.067483);
		
		houses.get(2).setPid(3l);
		houses.get(2).setName("c");
		houses.get(2).setLongitude(118.773704);
		houses.get(2).setLatitude(32.067583);
		
		houses.get(3).setPid(4l);
		houses.get(3).setName("d");
		houses.get(3).setLongitude(118.773204);
		houses.get(3).setLatitude(32.067183);
		
		houses.get(4).setPid(5l);
		houses.get(4).setName("e");
		houses.get(4).setLongitude(118.773104);
		houses.get(4).setLatitude(32.067083);
		return houses;
	}
	
	public List<Area> getHousesByCluster(School school, String type){
		List<House> houses = this.getHouseBySchool(school);
		List<Area> areas = new ArrayList<Area>();
		int base = 2;
		int size = houses.size();
		int clusterCount = 1;
		if(!(size < 2)){
			clusterCount = size/10 + base;
		}
		ArrayList<ArrayList<IClusterNode>> nodeClusters = getClusterService().getNodeClusters(houses, clusterCount);
		for(ArrayList< IClusterNode> nodesCluster : nodeClusters){
			Area area = getClusterService().getNodeArea(nodesCluster, type);
			if(area != null){
				areas.add(area);
				//ToDo: persist the areas info
			}
		}
		return areas;
	}
	
	public ArrayList<School> searchSchools(String city, String key){
		if(this.getStringMatcher().checkKey(key) == false)
			return null;
		ArrayList<School> schoolInfos = new ArrayList<School>();
		List<School> schools = this.getAllSchoolsByCity(city);
		for(School school : schools){
			if(this.getStringMatcher().isMatch(key, school.getName(), school.getLetters()))
				schoolInfos.add(school);
		}
		return schoolInfos;
	}

}
